const def = ['common', 'dataSource', 'socket', 'ajax', 'src/components/navigate.js'];
define(def, function(Common, dataSource, socket, ajax, menu) {
    const util = Common.util;
    const { routes } = dataSource;
    const Com = util.Class(Common);
    const com = new Com();

    console.log('-----------menu---------', menu);
    console.log('util', util);

    Com.include({
        init() {
            this.resetFont();

            if (this.isLogin()) {
                this.loadTempByHash();
                // this.initBackground();
                this.getUserInfo();
                this.initSocket();
            } else {
                this.routeTo('login');
            }

            window.addEventListener('resize', this.resetFont)
        },

        /**
         * 初始化 socket
         */
        initSocket() {
            socket.track(999).then(data => {
                let keys = Object.keys(data);
                if (keys.length) {
                    keys.forEach((item,index) => {
                        /*if (data[item] > 0) {
                            $("#icon_notic").addClass("new-notic")
                        }
                        $("#jnotic_notic .notic-li").eq(index).find("span").text(data[item])*/
                    })
                }
            })
        },

        /**
         * 初始化 背景
         */
        initBackground() {
            const $bg = $("#background");
            $bg.load('src/components/background/background.html', this.proxy(menu.initMenus, $bg));
        },

        /**
         * 重置字体大小
         */
        resetFont() {
            const widthScreen = $(window).width();
            const font = widthScreen / 1920 * 16;
            const htmlFont = document.getElementsByTagName('html')[0];

            htmlFont.style.fontSize = `${ font }px`;
            htmlFont.style.lineHeight = `${ font * 1.5 }px`;
        },

        getUserInfo() {
            ajax.getUserInfo().then(res => {
                this.setStore({userInfo: res.data || {}});
                $('#icon_user').text(res.data.userName);
            })
        },

        /**
         * 绑定事件
         */
        bindEvent() {

        },

        onRootLoaded(ctx, html, ...other) {
            // console.log(ctx, html, other);
            // console.log(this)
        },

        /**
         * 根据 哈希加载模板
         * 根据模板显示不同页面
         * @param key
         */
        loadTempByHash(key) {
            const routeKey = this.getRoute(key);
            const route = routes[routeKey] || routes.home;
            const $root = $("#root");

            require([`${route}.js`], Ctrl => new Ctrl(route))
            // $root.load(routes[routeKey] || routes.home, this.proxy(this.onRootLoaded, $root));
        },
    });

    return com
});



/*

common = {
    init() {
        this.Authorization = window.localStorage.getItem("auth_token") || '';
        // 绑定dom对象
        this.obj = {
            searchText: $('#searchText'),
        };
        // 声明全局变量
        this.vars = {
            titName:'',
        };
        this.loadFont();
        this.isLogin();
        window.onresize = this.loadFont;
        this.loadInner();
        // this.loadBackground();
        this.initSocket();
        this.btnmenu();
    },

    initSocket() {
        jrequest.jsocketTrack(999,{}).then(data => {
            let keys = Object.keys(data);
            console.log('jsocketTrack', data);
            if (keys.length) {
                keys.forEach((item,index) => {
                    //
                    if (data[item] > 0) {
                        $("#icon_notic").addClass("new-notic")
                    }
                    $("#jnotic_notic .notic-li").eq(index).find("span").text(data[item])
                })
            }
        })
    },

    loadFont(){
        const widthScreen = $(window).width();
        const font = widthScreen / 1920 * 16;
        const htmlFont = document.getElementsByTagName('html')[0];
        htmlFont.style.fontSize = `${ font }px`;
        htmlFont.style.lineHeight = `${ font * 1.5 }px`;
    },

    isLogin() {
        var $leftMenu = $(".head_mleft ul");
        var $rightMenu = $(".head_mright ul");
        var isLogin = window.localStorage.getItem("loginStatus");

        /!*if (!isLogin) {
            window.location.href = "login.html";
            return
        }*!/

        ajax.getMenus().then(menuData => {
            const menuList = ['<li data-href="home">首页</li>'];
            for(let i = 0, len = menuData.length; i < len; i++) {
                menuList.push(`<li>${menuData[i].menuName}</li>`);
            }

            /!**
             * 如果 超过 3 个，则加到右边
             *!/
            if (menuData.length > 3) {
                // + 首页 = 4
                $rightMenu.html(menuList.splice(4));
            }

            $leftMenu.html(menuList);

            // 默认有一个，表示首页
            const domMenu = ['<ul></ul>'];

            for (i = 0; i < menuData.length; i++) {
                const menuItem = menuData[i].menuItem;
                const subMenu = [];
                for(let j = 0, length = menuItem.length; j < length; j++) {
                    const submenu = menuItem[j];
                    const href = menus[submenu.menuName];
                    if (href) {
                        subMenu.push(`<li data-href="${href}">${submenu.menuName.substring(0,6)}"</li>`)
                    }
                }
                domMenu.push(`<ul>${subMenu.join('')}</ul>`);
            }

            $('.head_menusleve').html(domMenu);
            this.btnEvent();
        });
        jrequest.get(`/api/sysUserInfo`,{}).then(res => {
            window.JuserInfo = res.data || {}
            $('#icon_user').text(res.data.userName)

        })
    },

    btnEvent() {
        var self = this;
        $(".head_mleft ul li").off("click").click(function() {
            const $this = $(this);
            const hash = window.location.hash;
            const $ul = $(".head_menusleve ul");
            const index = $this.index();

            $this.addClass("head_active").siblings("li").removeClass("head_active");
            $this.parents(".head_menu").find(".head_mright ul li").removeClass("head_active");
            $ul.eq(index).toggle();
            $ul.eq(index).show().siblings("ul").hide();

            const href = $this.data("href");

            if (href) {
                window.location.hash = href;
            }

            if (hash && hash !== '#home' && index === 0) {
                self.loadInner(href);
            }

            $('.heaad_secord').css({ left: `${8 + index * 7.56}%` });

            // 删除 子列表 获得焦点的样式
            $(".head_menusleve ul li").removeClass("head_leveActive");

            creatliEvent();

            $ul.eq(index).mouseover(() => {
                $ul.eq(index).show().siblings("ul").hide();
            }).mouseleave(function(){
                $ul.eq(index).hide();
            })
        });
        $(".head_mright ul li").off("click").click(function(){
            var index=$(this).index();
            // alert(index+4)
            $(this).addClass("head_active").siblings("li").removeClass("head_active");
            $(this).parents(".head_menu").find(".head_mleft ul li").removeClass("head_active");
            $(".head_menusleve ul").eq(index+4).toggle();
            $(".head_menusleve ul").eq(index+4).show().siblings("ul").hide();
            var Iindex=$("s.head_menusleve ul").eq(index).find("li").length;
            $(".heaad_secord").css({height:Iindex*70+"%",left:[42.4+(index+3)*7.56+"%"]});
            $(".head_menusleve ul li").removeClass("head_leveActive");
            creatliEvent()
            $(".head_menusleve ul").eq(index+4).mouseover(function(){
                $(".head_menusleve ul").eq(index+4).show().siblings("ul").hide();
            }).mouseleave(function(){
                $(".head_menusleve ul").eq(index+4).hide();
            })
        });
        function creatliEvent(){
            $(".head_menusleve ul li").off("click").click(function(){
                var index=$(this).index();
                $(this).addClass("head_leveActive").siblings("li").removeClass("head_leveActive");
                var sId = $(this).data("href");
                window.location.hash =sId;
                self.loadInner(sId);
                $(".head_mleft ul li").each(function(i){
                    if($(".head_mleft ul li").eq(i).hasClass("head_active")){
                        $(".head_menusleve ul").eq(i).hide();
                    }
                });
                $(".head_mright ul li").each(function(i){
                    if($(".head_mright ul li").eq(i).hasClass("head_active")){
                        $(".head_menusleve ul").eq(i+4).hide();
                    }
                })
            });
            $(".head_menusleve ul li").mouseover(function() {
                $(this).addClass("head_leveActive").siblings("li").removeClass("head_leveActive");
            })
        }
        // 消息提醒
        $("#jnotic_notic").hide();
        $("#jnotic_user").hide();
        $(document).click(function (event) {
            var x1 = $('.jicon-notic'); // 设置目标区域
            // console.log(event)
            if (!x1.is(event.target) && x1.has(event.target).length === 0) {
                // $("#jnotic_user").hide()
                $("#jnotic_notic").hide()
            }
        });
        $("#icon_user").click(() => {
            let sId = 'personalManagement';
            window.location.hash =sId;
            self.loadInner(sId);
            // $("#jnotic_user").show()
            // $('#userInfo_userName').text(window.JuserInfo.userName)
            // $('#userInfo_realName').text(window.JuserInfo.realName)
        });
        $("#jnotic_notic .notic-li").click(function() {
            var sId = $(this).data("href");
            if(sId === 'platformHistory') {
                jrequest.get("/api/clearRealRemind",{}).then(res => {
                    $("#icon_notic").removeClass("new-notic")
                })
            }
            window.location.hash =sId;
            self.loadInner(sId);
            $("#jnotic_notic").hide()
        });
        $("#icon_notic").click(function() {
            $("#jnotic_notic").show()
        })
    },

    loadBackground () {
        // $("#background").load('src/components/background/background.html');
    },

    loadInner(sId) {
        // $('#body_containner').hide();
        var hash = window.location.hash;
        var routeKey = hash ? hash.substr(1) : hash;

        // $(".containner").load(routes[routeKey] || routes.home);
        // setTimeout(function() { $('#body_containner').show();}, 500);
    },
};
*/

